<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" style="text-align: left">
      <el-form-item label="角色名称">
        <el-input v-model="formInline.roleName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getrolemanage">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="addrolemanage">新增</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="新增" :visible.sync="dialogaddVisible" width="30%">
      <el-form :model="formData">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="formData.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序" :label-width="formLabelWidth">
          <el-input v-model="formData.sortId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="formData.isDelete" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="postrole">保存</el-button>
        <el-button type="primary" @click="dialogaddVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="dialogputVisible" width="30%">
      <el-form :model="formData">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="formData.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序" :label-width="formLabelWidth">
          <el-input v-model="formData.sortId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="formData.isDelete" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="formData.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="putrole">保存</el-button>
        <el-button type="primary" @click="dialogputVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="分配菜单权限" :visible.sync="dialogtreeVisible" width="30%">
      <el-tree :data="data" ref="permission" node-key="id" :props="defaultProps" default-expand-all show-checkbox
        @node-click="handleNodeClick"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="postrolemenu">保存</el-button>
        <el-button type="primary" @click="dialogtreeVisible = false">取消</el-button>
      </span>
    </el-dialog>
    <br>
    <el-table border ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center"> </el-table-column>
      <el-table-column prop="id" label="角色编号" align="center"> </el-table-column>
      <el-table-column prop="roleName" label="角色名称" align="center"> </el-table-column>
      <el-table-column prop="sortId" label="显示顺序" align="center"> </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isDelete" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="角色备注" align="center"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-link type="warning" @click="putrolemanage(scope.row)">编辑</el-link>
          <el-link type="danger" @click="deleterolemanage(scope.row)">删除</el-link>
          <el-link type="primary" @click="addrolemenu(scope.row)">分配菜单</el-link>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex" :page-sizes="[2, 4, 6, 8]" :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="this.count">
    </el-pagination>
  </div>
</template>
<script>
import {
  getrolemanageapi,
  addrolemanageapi,
  updatrolemanageapi,
  deleterolemanageapi,
} from "@/api/sysManage/RoleManage";
import { getmenumanagetreeapi } from "@/api/sysManage/MenuManage";
import { getrolemenuidapi, addrolemenuapi } from "@/api/sysManage/RoleMenu";
export default {
  data() {
    return {
      tableData: [],
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        roleName: "",
      },
      count: 0,
      dialogaddVisible: false,
      dialogputVisible: false,
      dialogtreeVisible: false,
      formLabelWidth: "120px",
      formData: {
        isDelete: true,
      },
      defaultProps: {},
      pp: {
        roleId: 0,
        menuId: [],
      },
      data: [{ id: 0 }],
    };
  },
  methods: {
    getrolemanage() {
      getrolemanageapi(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formInline.pageSize = val;
      this.getrolemanage();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formInline.pageIndex = val;
      this.getrolemanage();
    },
    resetForm() {
      this.formInline.roleName = "";
    },
    deleterolemanage(row) {
      if (confirm("记录数据删除将无法恢复，确认删除吗？")) {
        deleterolemanageapi(row).then((res) => {
          if (res.data.message == "操作成功") {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.getrolemanage();
          } else {
            this.$message({
              message: "删除失败",
              type: "error",
            });
          }
        });
      }
    },
    addrolemanage() {
      this.dialogaddVisible = true;
    },
    postrole() {
      addrolemanageapi(this.formData).then((res) => {
        if (res.data.message == "操作成功") {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.getrolemanage();
          this.dialogaddVisible = false;
        } else {
          this.$message({
            message: "添加失败",
            type: "error",
          });
        }
      });
    },
    putrolemanage(row) {
      this.dialogputVisible = true;
      this.formData = row;
    },
    putrole() {
      updatrolemanageapi(this.formData).then((res) => {
        if (res.data.message == "操作成功") {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.getrolemanage();
          this.dialogputVisible = false;
        } else {
          this.$message({
            message: "保存失败",
            type: "error",
          });
        }
      });
    },
    handleNodeClick() { },
    addrolemenu(row) {
      this.dialogtreeVisible = true;
      getmenumanagetreeapi().then((res) => {
        this.data = res.data;
      });
      let ids = [];
      this.pp.roleId = row.id;
      getrolemenuidapi(row).then((res) => {
        res.data.forEach((element) => {
          ids.push(element.menuId);
        });
        this.$nextTick(() => {
          this.$refs.permission.setCheckedKeys(ids);
        });
      });
    },
    postrolemenu() {
      let ids = [];
      this.$refs.permission.getCheckedNodes().forEach((element) => {
        ids.push(element.id);
      });
      this.pp.menuId = ids;
      addrolemenuapi(this.pp).then((res) => {
        if (res.data > 0) {
          this.$message.success("分配成功");
          this.dialogtreeVisible = false;
          this.getrolemanage();
        } else {
          this.$message.success("分配失败");
        }
      });
    },
  },
  created() {
    this.getrolemanage();
  },
};
</script>
<style></style>